<?php
class upload
{
	private $files = array();
	private $post_files;
	private $content_type;
	private $allowed;
	private $size;
	private $upload_dir;
	private $pdo;
	private $ext;
	
	private $error = array('too_large'=>'文件太大，您上传的文件最大为2M',
							'missing_dir'=>'没有找到目标路径',
							'is_not_dir'=>'路径不存在',
							'no_write_perm'=>'没有写的权限',
							'no_select_file'=>'没有选择要上传的文件',
							'bad_form'=>'表单没有包含method="post" enctype="multipart/form-data"',
							'e_move_file'=>'移动文件失败',
							'file_exests'=>'目标文件存在',
							'not_allow_extensin'=>'不能上传此文件类型',
							'partial'=>'文件只上传了一部分',
							'error'=>'上传失败'
							);
	
	public function __construct() {
		 $this->post_files = $_FILES;
         if (isset($_SERVER['CONTENT_TYPE'])) {
             $this->content_type = $_SERVER['CONTENT_TYPE'];
         }
         
         if(file_exists(BASEPATH.'db/dbBase'.EXT)) {
			require_once(BASEPATH.'db/dbBase'.EXT);
			$this->pdo = dbBase::getInstance()->get();		
		}
		
	}
	
	public function getError($item) {
		die($this->error[$item]);
	}
	
	public function filesUpload($file,$user_name,$dir_id,$user_id) {
		return $this->_buildFiles($this->post_files[$file],$user_name,$dir_id,$user_id);
	}
	
	
	public function checkFiles() {
		if(count($this->post_files < 1)) {
			return $this->getError('no_user_file');
		}
		
		$files = array();
		$size = 0;
		foreach($this->post_files as $userfile=>$value) {
			if(is_array($value['name'])) {
				foreach($value['name'] as $key => $value) {
					$size += $value['size'][$key];
				}
			} else {
				$size = $value['size'];
			}
			
			if($size == 0) {
				$this->getError('no_user_file');
			}
		}
	}	
	
	//
	
	private function _buildFiles($files,$user_name,$dir_id,$user_id) {
		
		if(!isset($this->content_type) || strpos($this->content_type,'multipart/form-data' !== 0)){
			return $this->getError('bad_form');
		}
		
		$count = count($files['name']);
		$this->allowed = config::$upload_allowed;
		$this->size = config::UPLOAD_SIZE;
		
		date_default_timezone_set('Asia/Shanghai');
		$now = date('Y-m-d H:i:s');
		$file_come_from = 'upload';
	
		$this->upload_dir = USERPATH .$user_name.'/upload';
		
		for($i = 0; $i < $count; $i++) {
			
			if($files['size'][$i] == 0) {
				return $this->getError('no_select_file');
			}
			
			if( ! in_array($files['type'][$i],$this->allowed)) {
				return $this->getError('not_allow_extensin');
			}
			
			//检测文件的大小
			if( $files['size'][$i] > ($this->size)) {
				//return $this->getError('too_large');
			}
			
			if(! is_dir($this->upload_dir)) {
				upload::mkdir_r($this->upload_dir);	
			}
			
			$sql = "INSERT INTO beauty_file SET file_name = :file_name,file_size = :file_size,file_type = :file_type,dir_id = :dir_id,user_id = :user_id,file_come_from = :file_come_from,file_create_time = :file_create_time";
			
			$stmt = $this->pdo->prepare($sql);
			
			
			$stmt->bindParam(':file_name',$files['name'][$i]);
			$stmt->bindParam(':file_size',$files['size'][$i]);
			$stmt->bindParam(':file_type',$files['type'][$i]);
			$stmt->bindParam(':dir_id',$dir_id);
			$stmt->bindParam(':user_id',$user_id);
			$stmt->bindParam(':file_come_from',$file_come_from);
			$stmt->bindParam(':file_create_time',$now);
			
			$stmt->execute();
			$insert_id = $this->pdo->lastInsertId();
			
			if($insert_id > 0) {
				if(is_uploaded_file($files['tmp_name'][$i])) {
					//$start = strrpos($files['name'][$i],'.');
					//$ext = substr($files['name'][$i],$start);
					$this->ext = strrchr($files['name'][$i],'.');
					if(in_array($this->ext,config::$image_app)) {
						
					}
				
					$files['url'][$i] = $this->upload_dir.'/'."$insert_id"."$this->ext";
					$result = move_uploaded_file($files['tmp_name'][$i],$files['url'][$i]); 
					if($result) {
						$file_url = config::UPLOAD_DIR .$user_name.'/upload'.'/'.$insert_id.$this->ext;
						$file_url = $this->pdo->quote($file_url);
						$insert_id = $this->pdo->quote($insert_id);
						$query = "UPDATE beauty_file SET file_url = $file_url WHERE file_id = $insert_id ";
						$row = $this->pdo->exec($query);
						if($row) return $file_url;
					}
				}
			}
		}
	}
	
/**
	 * 建立目录文件夹
	 * @param $dirName
	 * @param $rights
	 * @return unknown_type
	 */
	public static function mkdir_r($dirName, $rights=0777){
    	$dirs = explode('/', $dirName);
    	$dir='';
    	foreach ($dirs as $part) {
        	$dir.=$part.'/';
        	if (!is_dir($dir) && strlen($dir)>0)
            	mkdir($dir, $rights);
   		 }
	}
	
		
	
	
	
}
	
?>
